package cn.tedu.csmall.product.controller;

import cn.tedu.csmall.product.pojo.dto.*;
import cn.tedu.csmall.product.pojo.vo.*;
import cn.tedu.csmall.product.service.AlbumService;
import cn.tedu.csmall.product.service.CategoryService;
import cn.tedu.csmall.product.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/category")
@Api(tags = "02.类别管理模块")
@Slf4j
public class CategoryController {
    @Autowired
    CategoryService service;

    /**
     * 添加相册
     *
     * @param
     * @return
     */
    @PostMapping("/add-new")
    @ApiOperation("添加类别")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addNew(@Valid CategoryAddNewDto categoryAddNewDto) {
        service.addNew(categoryAddNewDto);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("根据ID删除类别")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "long"),
    })
    public JsonResult<Void> delete(@PathVariable @Range(min = 1) Long id) {
        service.delete(id);
        return JsonResult.ok();
    }

//    http://localhost:8080/categorie/list-by-parent?parentId=0
    @GetMapping("/list-by-parent")
    @ApiOperation("根据父级查询子级列表")
    @ApiOperationSupport(order = 420)
    public JsonResult<List<CategoryListItemVo>> list(Long parentId) {
        List<CategoryListItemVo> list = service.listByParentId(parentId);
        return JsonResult.ok(list);
    }

    @PostMapping("/{id:[0-9]+}/disable")
    @ApiOperation("禁用类别")
    @ApiOperationSupport(order = 311)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "long")
    })
    public JsonResult<Void> setDisable(@PathVariable @Range(min = 1, message = "请提交有效的ID值！") Long id) {
        log.debug("开始处理【禁用类别】的请求，参数：{}", id);
        service.setDisable(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/enable")
    @ApiOperation("启用类别")
    @ApiOperationSupport(order = 310)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "long")
    })
    public JsonResult<Void> setEnable(@PathVariable @Range(min = 1, message = "请提交有效的ID值！") Long id) {
        log.debug("开始处理【启用类别】的请求，参数：{}", id);
        service.setEnable(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/isDisplay")
    @ApiOperation("显示类别")
    @ApiOperationSupport(order = 310)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "long")
    })
    public JsonResult<Void> setIsDisplay(@PathVariable @Range(min = 1,message = "请提交有效的ID值！") Long id){
        service.setIsDisplay(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/notDisplay")
    @ApiOperation("不显示类别")
    @ApiOperationSupport(order = 310)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "long")
    })
    public JsonResult<Void> setNotDisplay(@PathVariable @Range(min = 1,message = "请提交有效的ID值！")Long id){
        log.debug("id{}",id);
        service.setNotDisplay(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/update")
    @ApiOperation("修改类别详情")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "long")
    })
    public JsonResult<Void> updateInfoById(@PathVariable @Range(min = 1, message = "请提交有效的ID值！") Long id,
                                           @Valid CategoryUpdateDto categoryUpdateDTO) {
        log.debug("开始处理【修改类别详情】的请求，ID：{}，新数据：{}", id, categoryUpdateDTO);
        service.updateInfoById(id, categoryUpdateDTO);
        return JsonResult.ok();
    }

    // http://localhost:9080/categories/9527
    @GetMapping("/{id:[0-9]+}")
    @ApiOperation("根据ID查询类别详情")
    @ApiOperationSupport(order = 410)
    public JsonResult<CategoryStandardVo> getStandardById(
            @PathVariable @Range(min = 1, message = "请提交有效的ID值！") Long id) {
        log.debug("开始处理【根据ID查询类别详情】的请求，参数：{}", id);
        CategoryStandardVo queryResult = service.getStandardById(id);
        return JsonResult.ok(queryResult);
    }
}
